﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using WinFormAssigment.Entities;
using WinFormAssigment.DAO;

namespace WinFormAssigment.BUS
{
    class StudentControl
    {
        public static Student GetStudentInFormation(string studentID) {            
            string sp = Query.getStudentInFormation;
            SqlParameter[] sqlParameter = Query.GetStudentInFormation(studentID);
            DataTable table = DatabaseInteraction.GetSelectTable(sp,sqlParameter);
            if (table.Rows.Count > 0)
            {
                string ID = Convert.ToString(table.Rows[0]["StudentID"]);
                string name = Convert.ToString(table.Rows[0]["Name"]);
                string gender = Convert.ToString(table.Rows[0]["Gender"]);
                DateTime dob = Convert.ToDateTime(table.Rows[0]["DOB"]);
                string address = Convert.ToString(table.Rows[0]["Address"]);
                string email = Convert.ToString(table.Rows[0]["Email"]);
                string phone = Convert.ToString(table.Rows[0]["Phone"]);
                string status = Convert.ToString(table.Rows[0]["Status"]);
                Student s = new Student()
                {
                    StudentID = ID,
                    Name = name,
                    Gender = gender,
                    Dob = dob,
                    Address = address,
                    Email = email,
                    Phone = phone,
                    Status = status
                };
                return s;
            }
            return null;            
        }
        public static DataTable GetAllStudentByStatus(string status) {
            string sp = Query.getAllStudentByStatus;
            SqlParameter[] sqlParameter = Query.GetAllStudentByStatus(status);
            DataTable table = DatabaseInteraction.GetSelectTable(sp,sqlParameter);
            return table;
        }
        public static DataTable GetAllStudentInDB() {
            string sp = Query.getAllStudentInDB;
            DataTable table = DatabaseInteraction.GetSelectTable(sp);
            return table;
        }
        public static bool InsertStudent(Student s) {
            string sp = Query.insertStudent;
            SqlParameter[] sqlParameter = Query.InsertStudent(s);
            bool b = DatabaseInteraction.InteractionData(sp,sqlParameter);
            return b;            
        }
        public static bool UpdateStudent(Student s) {
            string sp = Query.updateStudent;
            SqlParameter[] sqlParameter = Query.UpdateStudent(s);
            bool b = DatabaseInteraction.InteractionData(sp, sqlParameter);
            return b;
        }
        public static bool DeleteStudent(string studentID) {
            string sp = Query.deleteStudent;
            SqlParameter[] sqlParameter = Query.DeleteStudent(studentID);
            bool b = DatabaseInteraction.InteractionData(sp, sqlParameter);
            return b;
        }
        public static DataTable SearchStudent(string name) {
            string sp = Query.searchStudent;
            SqlParameter[] sqlParameter = Query.SearchStudent(name);
            DataTable table = DatabaseInteraction.GetSelectTable(sp,sqlParameter);
            return table;
        }
    }
}
